import { ErrorObserver } from './performance/error.ts';
// import { BehaviorObserver } from './performance/behavior.ts';
const performanceTypes = [
  'fcp', 'navigation', 'resource', 'longtask', 'lcp', 'fid', 'paint'] as const;

type ObserverType = typeof performanceTypes[number];
type observers = {
    [key in ObserverType]?: PerformanceObserver;
}
export class PageObserver extends ErrorObserver {
    static instance: PageObserver | null = null; // 静态属性存储实例
    private observers: observers = {};
    static getInstance():PageObserver {
      if (!PageObserver.instance) {
        PageObserver.instance = new PageObserver();
      }
      return PageObserver.instance;
    }
      private constructor() {
        super();
        this.handleUnhandledrejection();
        this.handleResourceError();
        this.handleJsError();
    }
    public addObserver(type: ObserverType) {
        switch (type) {
            case 'fcp':
                this.observers.paint = new PerformanceObserver((list) => {
                  for (const entry of list.getEntries()) {
                    if (entry.name === 'first-contentful-paint') {
                        const json = entry.toJSON();
                        const reportData = {
                            ...json,
                            type: 'performance',
                            subType: entry.name,
                            pageUrl: window.location.href,
                        }
                        this.lazyReportBatch(reportData);
                    }
                  }
              });
              this.observers.paint.observe({type: 'paint', buffered: true});
              // this.observers.paint.disconnect();
                break;
            case 'navigation':
                this.observers.navigation = new PerformanceObserver((list) => {
                  for (const entry of list.getEntries()) {
                    const json = entry.toJSON();
                    const reportData = {
                        ...json,
                        type: 'performance',
                        subType: entry.name,
                        pageUrl: window.location.href,
                    }
                    this.lazyReportBatch(reportData);
                  }
              });
                break;
            case 'resource':
                this.observers.resource = new PerformanceObserver((list) => {
                  for (const entry of list.getEntries()) {
                    const json = entry.toJSON();
                    const reportData = {
                        ...json,
                        type: 'performance',
                        subType: entry.name,
                        pageUrl: window.location.href,
                    }
                    this.lazyReportBatch(reportData);
                  }
              });
                break;
            case 'longtask':
                this.observers.longtask = new PerformanceObserver((list) => {
                  for (const entry of list.getEntries()) {
                    const json = entry.toJSON();
                    const reportData = {
                        ...json,
                        type: 'performance',
                        subType: entry.name,
                        pageUrl: window.location.href,
                    }
                    this.lazyReportBatch(reportData);
                  }
              });
                break;
            case 'lcp':
                this.observers.lcp = new PerformanceObserver((list) => {
                  for (const entry of list.getEntries()) {
                    const json = entry.toJSON();
                    const reportData = {
                        ...json,
                        type: 'performance',
                        subType: entry.name,
                        pageUrl: window.location.href,
                    }
                    this.lazyReportBatch(reportData);
                  }  
                })
                this.observers.lcp.observe({type: 'largest-contentful-paint', buffered: true});
                break;
            default:
                throw new Error(`Unsupported observer type: ${type}`);
        }
    }
}

